package org.veridiandynamics.client.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import org.veridiandynamics.server.domain.Patient;

/**
 * This class creates invisible frames and takes on panels to it depending on 
 * whether the user logged in as a patient, doctor, nurse or admin.
 * 
 * @author Lee
 */
public class MainWindow
{
	public static JFrame frame;
	private int role;
	public JTabbedPane tPane;
	public JButton logOut;
	private String username;
	private Object logdummy;
	public static JLabel welcomeMessage;
	
	/**
	 * Constructor for the main windows - initializes the frame.
	 * 
	 * @param role
	 * @param username
	 */
	public MainWindow(int role, String username, final Object logdummy)
	{
		this.role = role;
		this.username = username;
		this.logdummy = logdummy;
		initialize(role);
	}
	
	/**
	 * Initializes the contents of the frame.
	 */
	private void initialize(int myRole) {
		frame = new JFrame("Doctor's Office Application");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.setLayout(new BorderLayout());
		JPanel northernPanel = new JPanel();
		welcomeMessage = new JLabel("Welcome");
		northernPanel.add(welcomeMessage);
		northernPanel.setPreferredSize(new Dimension(0, 50));
		northernPanel.setBackground(new Color(200, 220, 200));
		frame.getContentPane().add(northernPanel, BorderLayout.NORTH);
		
		JPanel centerPanel = new JPanel();
		tPane = new JTabbedPane();
		centerPanel.add(tPane);
		frame.getContentPane().add(centerPanel);
		
		JPanel southernPanel = new JPanel();
		JButton help = new JButton("Help");
		southernPanel.add(help);
		logOut = new JButton("LogOut");
		logOut.addActionListener(new ActionListener()
		{
			/**
			 * This method listens to the logout button and handles logging out.
			 */
			public void actionPerformed(ActionEvent event)
			{
				logOut();
			}
		});
		southernPanel.add(logOut);
		southernPanel.setPreferredSize(new Dimension(0, 50));
		southernPanel.setBackground(new Color(200, 220, 200));
		frame.getContentPane().add(southernPanel, BorderLayout.SOUTH);
		
		switch(myRole)
		{
			case 0: //patient
			{
				System.out.println(logdummy);
				PatientPanel pPanel = new PatientPanel(username, logdummy);
				tPane.add(pPanel, "Patient");
				break;
			}
		    
			case 1: //nurse
			{
				NursePanel nPanel = new NursePanel();
				PatientPanel pPanel = new PatientPanel(username, logdummy);
				tPane.add(nPanel, "Nurse");
				tPane.add(pPanel, "Patient");
				break;
			}
		    
			case 2: // administrator
			{	
				AdminPanel aPanel = new AdminPanel();
				DoctorPanel dPanel = new DoctorPanel();
				NursePanel nPanel = new NursePanel();
				PatientPanel pPanel = new PatientPanel(username, logdummy);
				tPane.add(aPanel, "Admin");
				tPane.add(dPanel, "Doctor");
				tPane.add(nPanel, "Nurse");
				tPane.add(pPanel, "Patient");
				break;
			}
			
			case 3: // doctor
			{	
				DoctorPanel dPanel = new DoctorPanel();
				NursePanel nPanel = new NursePanel();
				PatientPanel pPanel = new PatientPanel(username, logdummy);
				tPane.add(dPanel, "Doctor");
				tPane.add(nPanel, "Nurse");
				tPane.add(pPanel, "Patient");
				break;
			}
		}  
		
		frame.setVisible(true);
		frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
	}
	
	/**
	 * This method closes the main window frame.
	 */
	public void close()
	{
		frame.dispose();
	}
	
	/**
	 * This method actually handles logging out of the system.
	 */
	public static void logOut()
	{
		frame.dispose();
		LoginScreen screen = new LoginScreen();
	}
}
